package org.micode.libs.wxapp.config.reader;

import org.micode.common.exception.DataException;
import org.micode.libs.wxapp.config.AppParamsHelper;
import org.micode.libs.wxapp.config.IWxMaPropReader;
import org.micode.libs.wxapp.vo.WxAppConfigVO;
import org.micode.libs.wxapp.vo.WxAppVO;
import org.apache.commons.lang3.StringUtils;

public abstract class BaseWxMaPropReader implements IWxMaPropReader {

    @Override
    public WxAppConfigVO loadConfigForApp(String appId) {
        WxAppVO appVO = getAppInfo(appId);
        if (null == appVO) {
            throw new DataException("Wechat applet (" + appId + ") unregistered!");
        }

        String params = appVO.getAppParams();
        if (!StringUtils.isAnyBlank(appId) && !StringUtils.isAnyBlank(params)) {
            WxAppConfigVO config = AppParamsHelper.readConfigVO(params);
            if (null != config) {
                config.setAppId(appId);
            }
            return config;
        }
        return null;
    }

    protected abstract WxAppVO getAppInfo(String appId);

}
